//
//  KuserInfo.m
//  Peanut
//
//  Created by Jim huang on 2018/3/30.
//  Copyright © 2018年 Jim_Peanut. All rights reserved.
//

#import "KuserInfo.h"
#import "NSFileManager+Util.h"
#import "KUserControlRequest.h"
#import "AppDelegate.h"
#import "DMNavigationController.h"
#import "DMLoginVC.h"
#import <AVFoundation/AVFoundation.h>
#import <AssetsLibrary/AssetsLibrary.h>

@implementation KuserInfo
//单例
+ (instancetype)shareInstance{
    static KuserInfo *instance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[KuserInfo alloc]init];
    });
    return instance;
}

//保存usermodel到本地
-(BOOL)archiveUserInfo
{
    [NSFileManager removeObjectByFileName:@"userInfoModel.v1"];
        return [NSFileManager saveObject:self.userModel withName:@"userInfoModel.v1"];
}
//从本地取出userModel
-(KUserModel *)unarchiveUserInfo
{
    self.userModel = [NSFileManager getObjectByFileName:@"userInfoModel.v1"];
    return self.userModel;
}

//更新用户信息并保存
-(void)updateUserModel:(void(^)(KUserModel *userModel))successBlock failure:(void(^)(void))failureBlock{
    

    
    if (self.isLogin) {
        
        KUserControlRequest * request = [[KUserControlRequest alloc]initGetUserDetail];
        
        MJWeakSelf
        [request newStartRequestWithCompletion:^(KBaseModel *baseModel) {
                NSString * sid = [KuserInfo shareInstance].userModel.sid;
            NSString * phone = [KuserInfo shareInstance].userModel.phone;
            NSString * password = [KuserInfo shareInstance].userModel.password;
            KUserBaseModel * userBaseModel = [KUserBaseModel modelWithJSON:baseModel.data];
            userBaseModel.partyView.password = password;
            userBaseModel.partyView.phone = phone;
            userBaseModel.partyView.sid = sid;
            
            weakSelf.userModel= userBaseModel.partyView;
            
            [weakSelf archiveUserInfo];
            
            !successBlock?:successBlock(userBaseModel.partyView);
            
        } failure:^(KBaseModel *errorModel) {
            
            [JLProgressHUDManager showErrorHud:errorModel.msg];
            !failureBlock?:failureBlock();
            
        }];
        
    }else{
        
    }
}

//退出登录
-(void)logout{
    
    [NSFileManager removeObjectByFileName:@"userInfoModel.v1"];
    AppDelegate* app = (AppDelegate*)[UIApplication sharedApplication].delegate;
    self.userModel = nil;
    [KuserInfo shareInstance].isLogin = NO;
    app.window.rootViewController = [[DMNavigationController alloc] initWithRootViewController:[DMLoginVC new]];
    

}

-(NSString *)currentVersion {
    return [[NSBundle mainBundle] infoDictionary][@"CFBundleShortVersionString"];
}

/**判断是否有相机权限*/
+ (BOOL)isCameraDeviceAvalable
{
    AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
    if (authStatus == AVAuthorizationStatusDenied || authStatus == AVAuthorizationStatusRestricted) {
        
        return NO;
    }
    
    return YES;
}

@end
